<template>
  <div class="app-container">
    <el-card>
      <el-form ref="list_q_ref" :model="list_q" inline>
        <el-form-item
          v-for="(item, index) in list_q_c"
          :key="index"
          v-bind="item.form_item_prop"
        >
          <component
            :is="item.component_name"
            v-bind="item.component_prop"
            v-model="list_q[item.form_item_prop.prop]"
            :placeholder="item.form_item_prop.label"
            v-on="item.handler"
          >
            <template v-if="item.component_name == 'el-select'">
              <el-option label="已下发" value="已下发"></el-option>
              <el-option label="未发布" value="未发布"></el-option>
            </template>
            <template v-else-if="item.component_name == 'el-upload'">
              <el-button>上传</el-button>
            </template>
            <template v-else-if="item.form_item_prop.label == '计划号'">
              <el-button slot="append" type="primary">选择</el-button>
            </template>
          </component>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="get_list">搜索</el-button>
          <el-button @click="reset_query_form">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="success" @click="open_add_dialog" size="mini"
        >新增</el-button
      >
      <el-table :data="list">
        <el-table-column
          v-for="item in list_c"
          :key="item.prop"
          v-bind="item"
        ></el-table-column>
        <el-table-column label="操作" width="200px" v-slot="{ row }">
          <el-button type="primary" size="mini" @click="open_edit_dialog(row)"
            >修改</el-button
          >
          <el-button type="danger" size="mini" @click="del_list_row(row)"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @size-change="
          (val) => {
            pager.limit = val;
            get_list();
          }
        "
        @current-change="
          (val) => {
            pager.page = val;
            get_list();
          }
        "
        :current-page="pager.page"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      :title="title"
      center
      :visible.sync="visible"
      @close="reset_form"
    >
      <el-form ref="list_e_ref" :model="list_e" inline>
        <el-form-item v-for="item in list_e_c" :key="item.prop" v-bind="item">
          <el-date-picker
            v-if="item.type == 'datetime'"
            v-model="list_e[item.prop]"
            :disabled="is_add ? false : is_disabled(item.disabled)"
            type="datetime"
            :placeholder="item.label"
          >
          </el-date-picker>
          <el-input
            v-else
            v-model="list_e[item.prop]"
            :disabled="is_add ? false : is_disabled(item.disabled)"
            :placeholder="item.label"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-table :data="detailList">
        <el-table-column
          v-for="item in detailList_c"
          :key="item.prop"
          v-bind="item"
        >
          <template
            v-if="item.prop === 'remark' && list_e.publishStatus == '未发布'"
            v-slot="{ row }"
          >
            <el-input
              type="textarea"
              :rows="3"
              resize="none"
              v-model="row.remark"
              placeholder="备注"
            ></el-input>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div>
          <el-button size="mini" @click="visible = false">取消</el-button>
          <el-button size="mini" type="primary" @click="submit">保存</el-button>
          <el-button size="mini" type="primary" @click="submit">提交</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getProductScheduleList, editProductSchedule, addProductSchedule } from '@/api_zc/mes/product-schedule'
// import { getMaterialCategoryList } from '@/api_zc/mes/bom'
import cloneDeep from 'lodash/cloneDeep'
export default {
  async mounted () {
    this.get_list()
    // const { data } = await getMaterialCategoryList()
    // this.list_q_c.find(item => item.component_name == 'el-cascader').component_prop.options = data
  },
  data () {
    return {
      list: [],
      pager: {
        page: 1,
        limit: 10,
        total: 0
      },
      list_c: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '流水号',
          prop: 'serialNo'
        },
        {
          label: '计划号',
          prop: 'planNo'
        },
        {
          label: '产品',
          prop: 'product'
        },
        {
          label: '型号',
          prop: 'productModel'
        },
        {
          label: '规格',
          prop: 'productSpecification'
        },
        {
          label: '单位',
          prop: 'productUnit'
        },
        {
          label: 'BOM',
          prop: 'bom'
        },
        {
          label: '生产线',
          prop: 'productionLine'
        },
        {
          label: '工艺',
          prop: 'process'
        },
        {
          label: '项目',
          prop: 'project'
        },
        {
          label: '待产数量',
          prop: 'waitingAmount'
        },
        {
          label: '已产数量',
          prop: 'producedAmount'
        },
        {
          label: '生产日期',
          prop: 'productionDate'
        },
        {
          label: '交货日期',
          prop: 'deliveryDate'
        },
        {
          label: '发布状态',
          prop: 'publishStatus'
        },
        {
          label: '领料状态',
          prop: 'materialStatus'
        }
      ],
      list_q: {
        serialNo: '',
        planNo: '',
        material: '',
        bom: '',
        productionLine: '',
        process: '',
        project: '',
        productionDate: [],
        productionStartDate: '',
        productionEndDate: '',
        deliveryStartDate: '',
        deliveryEndDate: '',
        deliveryDate: [],
        publishStatus: ''
      },
      list_q_c: [
        { form_item_prop: { label: '流水号', prop: 'serialNo' }, component_name: 'el-input' },
        // { label: '流水号', prop: 'serialNo', c: 'el-input-number', min: 1 },
        { form_item_prop: { label: '计划号', prop: 'planNo' }, component_name: 'el-input' },
        { form_item_prop: { label: '物料', prop: 'material' }, component_name: 'el-input' },
        { form_item_prop: { label: 'BOM', prop: 'bom' }, component_name: 'el-input' },
        { form_item_prop: { label: '生产线', prop: 'productionLine' }, component_name: 'el-input' },
        { form_item_prop: { label: '工艺', prop: 'process' }, component_name: 'el-input' },
        { form_item_prop: { label: '项目', prop: 'project' }, component_name: 'el-input' },
        { form_item_prop: { label: '生产日期', prop: 'productionDate' }, component_name: 'el-date-picker', component_prop: { type: "datetimerange", 'range-separator': "至", "start-placeholder": "开始日期", "end-placeholder": "结束日期" }, },
        { form_item_prop: { label: '交货日期', prop: 'deliveryDate' }, component_name: 'el-date-picker', component_prop: { type: "datetimerange", 'range-separator': "至", "start-placeholder": "开始日期", "end-placeholder": "结束日期" }, },
        { form_item_prop: { label: '状态', prop: 'publishStatus' }, component_name: 'el-select' },
        // { form_item_prop: { label: '状态', prop: 'publishtatus' }, component_name: 'el-cascader', component_prop: { options: [], props: { label: 'name', value: 'id', emitPath: false, checkStrictly: true } }, handler: { change: this.handleChange } },
        // { form_item_prop: { label: '文件上传', prop: null }, component_name: 'el-upload', component_prop: { action: 'https://jsonplaceholder.typicode.com/posts/', 'before-upload': this.on_preview }, handler: { change: this.handleChange } },
      ],
      list_e_c: [
        { label: '计划号', prop: 'planNo', required: true, disabled: false },
        { label: '项目', prop: 'project', required: false, disabled: true },
        { label: '产品', prop: 'product', required: false, disabled: true },
        { label: '规格', prop: 'productSpecification', required: false, disabled: true },
        { label: '型号', prop: 'productModel', required: false, disabled: true },
        { label: '单位', prop: 'productUnit', required: false, disabled: true },
        { label: 'BOM', prop: 'bom', required: true, disabled: false },
        { label: '生产线', prop: 'productionLine', required: true, disabled: false },
        { label: '工艺', prop: 'process', required: true, disabled: false },
        { label: '待产数量', prop: 'waitingAmount', required: true, disabled: true },
        { label: '已产数量', prop: 'producedAmount', required: false, disabled: true },
        { label: '生产日期', prop: 'productionDate', type: 'datetime', required: false, disabled: false },
        { label: '交货日期', prop: 'deliveryDate', type: 'datetime', required: false, disabled: true },
        { label: '状态', prop: 'publishStatus', required: false, disabled: true },
        { label: '备注', prop: 'remark', required: false, disabled: false },
      ],
      list_e: {
        bom: null,
        deliveryDate: null,
        description: null,
        name: null,
        planNo: null,
        powerConsumption: null,
        process: null,
        producedAmount: null,
        product: null,
        productModel: null,
        productSpecification: null,
        productUnit: null,
        productionDate: null,
        productionLine: null,
        project: null,
        publishStatus: null,
        remark: null,
        waitingAmount: null,
      },
      detailList: [],
      detailList_c: [
        {
          label: '物料',
          prop: 'material'
        },
        {
          label: '型号',
          prop: 'model'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '单位',
          prop: 'unit'
        },
        {
          label: '需求数量',
          prop: 'demandAmount'
        },
        {
          label: '已用数量',
          prop: 'usedAmount'
        },
        {
          label: '剩余数',
          prop: 'remainAmount'
        },
        {
          label: '备注',
          prop: 'remark',
          width: '200px'
        },
      ],
      visible: false,
      title: '',
      is_add: false
    }
  },
  methods: {
    async get_list () {
      if (!this.list_q.productionDate) {
        return
      }
      if (!this.list_q.deliveryDate) {
        return
      }
      [this.list_q.productionStartDate, this.list_q.productionEndDate] = this.list_q.productionDate;
      [this.list_q.deliveryStartDate, this.list_q.deliveryEndDate] = this.list_q.deliveryDate
      const { data: { total, content } } = await getProductScheduleList({ ...this.list_q, page: this.pager.page, limit: this.pager.limit })
      this.list = content
      this.pager.total = total
    },
    reset_form () {
      console.log(1)
      this.visible = false
      this.$refs.list_e_ref.resetFields()
    },
    reset_query_form () {
      this.$refs.list_q_ref.resetFields()
      this.get_list()
    },
    is_disabled (disabled) {
      if (this.list_e.publishStatus == '已发布') {
        return true
      }
      return disabled
    },
    open_edit_dialog (row) {
      this.visible = true
      this.is_add = false
      this.title = '修改生产计划'
      this.$nextTick(() => {
        Object.assign(this.list_e, row)
        this.detailList = cloneDeep(row.detailList)
      })
    },
    open_add_dialog () {
      this.visible = true
      this.is_add = true
      this.title = '添加生产计划'
    },
    submit () {
      this.$refs.list_e_ref.validate(async (callback) => {
        if (!callback) {
          return this.$message.error('请完善信息')
        }
        if (this.is_add) {
          await addProductSchedule({
            ...this.list_e, detailList: this.detailList,
            deliveryDate: new Date(this.list_e.deliveryDate),
            productionDate: new Date(this.list_e.productionDate),
          })
          this.$message.success('添加成功')
        } else {
          await editProductSchedule({
            ...this.list_e, detailList: this.detailList,
            deliveryDate: new Date(this.list_e.deliveryDate),
            productionDate: new Date(this.list_e.productionDate),
          })
          this.$message.success('修改成功')
        }
        this.visible = false
        this.get_list()
      })
    }
  },
}
</script>

<style>
</style>